package com.tjshop.android;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.actionbarsherlock.view.MenuItem;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.util.HttpUtil;
import com.tjshop.android.util.MD5Utils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 活动详情界面。
 * 
 * @author FIMH
 * @since 2015-01-03
 */
public class EventDetailActivity extends AbsBaseActionBarActivity implements
		View.OnClickListener, Handler.Callback {

	private WebView mWebView;
	private Context mContext;
	private Handler handler;

	private String Event_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_detail);

		mContext = this;
		handler = new Handler(this);

		mWebView = (WebView) findViewById(R.id.event_detail_content);
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		Bundle data = getIntent().getExtras();
		Event_id = data.getString("coupon_id");
		String url = AppConfig.ActivityDetailUrl + "?id=" + Event_id;
		mWebView.loadUrl(url);

		findViewById(R.id.event_detail_apply).setOnClickListener(this);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish(); // 因为有可能从不同的Activity调用，所以finish()返回即可
			return true;
		default:
			return super.onOptionsItemSelected(item);

		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.event_detail_apply:
			if (GlobeVars.isLogin) {
				SignActivity(Event_id, GlobeVars.UserInfos.getId());
			} else {
				Toast.makeText(mContext, "请先登录", Toast.LENGTH_SHORT).show();
			}

			break;
		default:
			break;
		}
	}

	void SignActivity(String Event_id, String user_id) {
		String md5 = MD5Utils.md5(Event_id + user_id + AppConfig.SignKey);
		final String param = "event_id=" + Event_id + "&user_id=" + user_id
				+ "&sign=" + md5;
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message message = new Message();
				String ret = HttpUtil
						.sendPost(AppConfig.SignActivityUrl, param);
				if (!ret.equals("")) {
					try {
						JSONObject json = new JSONObject(ret);
						if (json.getInt("code") == 0) {
							message.what = 0;
							handler.sendMessage(message);
						} else {
							message.what = 1;
							handler.sendMessage(message);
						}
					} catch (JSONException e) {
						message.what = 1;
						handler.sendMessage(message);
						e.printStackTrace();
					}
				}

			}
		}).start();
	}

	@Override
	public boolean handleMessage(Message message) {
		switch (message.what) {
		case 0:
			Toast.makeText(mContext, "参加活动成功!", Toast.LENGTH_SHORT).show();
			break;
		case 1:

			Toast.makeText(mContext, "参加活动失败:", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
		return false;
	}
}
